package com.leaf.collegeidleapp;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.leaf.collegeidleapp.adapter.ChatAdapter;
import com.leaf.collegeidleapp.bean.Commodity;
import com.leaf.collegeidleapp.bean.Star;
import com.leaf.collegeidleapp.model.ChatModel;
import com.leaf.collegeidleapp.model.ItemModel;
import com.leaf.collegeidleapp.util.CommodityDbHelper;
import com.leaf.collegeidleapp.util.StarDBHelper;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    ChatAdapter adapter;
    private EditText et;
    private TextView tvSend;
    private String content;
    private TextView tvchange;
    private Boolean Boolnum = false;
    private Number numbers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        recyclerView = (RecyclerView) findViewById(R.id.recylerView);
        et = (EditText) findViewById(R.id.et);
        tvSend = (TextView) findViewById(R.id.tvSend);
        //切换聊天者
        tvchange = (TextView) findViewById(R.id.change);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(ChatActivity.this, LinearLayoutManager.VERTICAL, false));

        recyclerView.setAdapter(adapter = new ChatAdapter());
        adapter.replaceAll(TestData.getTestAdData());
        initData();


        //关注按钮
        ImageButton ibStar = findViewById(R.id.ib_star);
        ibStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                StarDBHelper dbHelper = new StarDBHelper(getApplicationContext(),StarDBHelper.DB_NAME,null,1);
                Star star = new Star();
                String stuId = getIntent().getStringExtra("stuId");
                star.setStuId(stuId);

                CommodityDbHelper dbHelper1 = new CommodityDbHelper(getApplicationContext(),CommodityDbHelper.DB_NAME,null,1);
                List<Commodity> myCommodities = new ArrayList<>();
                myCommodities = dbHelper1.readMyCommoditiesPhone(stuId);
                star.setPhone(myCommodities.get(0).getPhone());
                dbHelper.addMyStar(star);
                Toast.makeText(getApplicationContext(),"成功关注了用户"+stuId,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                content = s.toString().trim();
            }
        });

        tvSend.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                ArrayList<ItemModel> data = new ArrayList<>();
                ChatModel model = new ChatModel();
                model.setIcon("http://img.my.csdn.net/uploads/201508/05/1438760758_6667.jpg");
                model.setContent(content);
//                model.setType();
//                data.add(new ItemModel(ItemModel.CHAT_B, model));
                if(Boolnum == false){
                    numbers = 1002 ;
                }
                else {
                    numbers = 1001;
                }
                data.add(new ItemModel((Integer) numbers, model));
                adapter.addAll(data);
                et.setText("");
                hideKeyBorad(et);
            }
        });

        //切换聊天对象
        tvchange.setOnClickListener(new View.OnClickListener(){
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                Boolnum = !Boolnum;
                if(Boolnum == false){
                    tvchange.setText("卖家");
                }
                else {
                    tvchange.setText("我");
                }
            }
        });

    }

    private void hideKeyBorad(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
    }

}
